const request = require("request");

let HttpUtil = {
    _get: url => {
        return new Promise((resolve, reject) => {
            request.get(url, (err, res, data) => {
                if(!err && res.statusCode == 200){
                    resolve(data);
                }else{
                    reject('请求异常，请检查服务.');
                }
            });
        })
    },
    _post: (url, json) => {
        /**
         * json --> application/json
         * form --> application/x-www-form-urlencoded
         * formData --> multipart/form-data
         * multipart --> multipart/related
         */
        let opts = {
            url,
            method: 'POST',
            json
        }

        return new Promise((resolve, reject) => {
            request.post(opts, (err, res, data) => {
                if(!err && res.statusCode == 200){
                    resolve(data);
                }else{
                    reject('Error: 请求异常，请检查服务.');
                }
            });
        })
    }
}
 
global.$http = HttpUtil;